/*
 * @(#)DmsFolderPickerTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.taglib;

import java.sql.Connection;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.framework.taglib.TagUtil;
import com.dcivision.framework.taglib.html.FormTag;
import com.dcivision.framework.taglib.html.StandardButtonTag;
import com.dcivision.framework.taglib.html.TextTag;
/**
    DmsFolderPickerTag.java

    <p>This class is to extend the TextTag provided by Apache Struts.<p>
    <p>Modification in "struts-html.tld"<br>

  <pre>
     [ADDED]
      &lt;tagclass&gt;com.dcivision.dms.taglib.DmsFolderPicker &lt;/tagclass&gt;
      &lt;attribute&gt;
      &lt;name&gt;hiddenRootID&lt;/name&gt;
      &lt;required&gt;true&lt;/required&gt;
      &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
      &lt;/attribute&gt;
      &lt;attribute&gt;
      &lt;name&gt;hiddenParentID&lt;/name&gt;
      &lt;required&gt;true&lt;/required&gt;
      &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
      &lt;/attribute&gt;
  </pre>
    </p>

  <p>USAGE:</p>
  <p>The following example shows the DmsFolderSelection usage. </p>
  <p><code>&lt;dms:multipleDocumentSelection property='fileList' <strong>rootID='&lt;%=rootID%&gt;' parentID='&lt;%=parentID%gt;'</strong>/&gt;</code></p>

  @author          Zoe Shum
  @company         DCIVision Limited
  @creation date   27/02/2004
  @version $Revision: 1.23.2.5 $
 */
public class DmsFolderPickerTag extends TextTag {

  public static final String REVISION = "$Revision: 1.23.2.5 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());
  String sHiddenRootID = "";
  String sHiddenParentID="";
  String sHiddenRootIDValue = "";
  String sHiddenParentIDValue = "";
  boolean disabled=false;
  String isMyFavorite = "";

  // in copy /move jsp page paramter to determine it calls its refresh js function
  String sRefreshCopyMove="";
  
  Integer arrayIndex = null;
  
  // to control to allow to choose compound document as result
  boolean allowCompoundDoc = true;
  boolean allowPaperDoc=true;
  boolean allowEmailDoc = true;
  boolean allowFormDoc=true;
  boolean allowFlowDoc = true;
  boolean allowPersonalOnly = false;
  boolean allowPublicOnly = false;
 
  // to control to allow show reset button
  boolean allowReset=false;

  public DmsFolderPickerTag() {
    super();
    this.setReadonly(true);
  }

  /**
   * release
   */
  public void release() {
    super.release();
    sHiddenRootID = null;
    sHiddenParentID = null;
    sHiddenRootIDValue = null;
    sHiddenParentIDValue = null;
    allowCompoundDoc = false;
    allowPaperDoc=false;
    allowEmailDoc = false;
    allowFormDoc=false;
    allowFlowDoc = false;
    this.setReadonly(true);
  }

  /** Getter for property rootID.
   * @return Value of property rootID.
   */
  public String getHiddenRootID() {
    return this.sHiddenRootID;
  }

  /** Setter for property rootID.
   * @param rootID New value of property rootID.
   */
  public void setHiddenRootID(String rootID) {
    this.sHiddenRootID = rootID;
  }

  /** Getter for property parentID.
   * @return Value of property parentID.
   */
  public String getHiddenParentID() {
    return this.sHiddenParentID;
  }

  /** Setter for property parentID.
   * @param parentID New value of property parentID.
   */
  public void setHiddenParentID(String parentID) {
    this.sHiddenParentID = parentID;
  }

  /** Getter for property rootIDValue.
   * @return Value of property rootID.
   */
  public String getHiddenRootIDValue() {
    return this.sHiddenRootIDValue;
  }

  /** Setter for property rootIDValue.
   * @param rootID New value of property rootID.
   */
  public void setHiddenRootIDValue(String rootIDValue) {
    this.sHiddenRootIDValue = rootIDValue;
  }

  /** Getter for property parentIDValue.
   * @return Value of property parentID.
   */
  public String getHiddenParentIDValue() {
    return this.sHiddenParentIDValue;
  }

  /** Setter for property parentIDValue.
   * @param parentID New value of property parentID.
   */
  public void setHiddenParentIDValue(String parentIDValue) {
    this.sHiddenParentIDValue = parentIDValue;
  }

  public String getRefreshCopyMove(){
    return this.sRefreshCopyMove;
  }

  public void setRefreshCopyMove(String refreshCopyMove){
    this.sRefreshCopyMove= refreshCopyMove;
  }
  
  public boolean getDisabled(){
    return this.disabled; 
  }
  
  public void setDisabled(boolean disabled){
    this.disabled=disabled; 
  }
  
  public void setAllowCompoundDoc(boolean allowCompoundDoc){
    this.allowCompoundDoc = allowCompoundDoc;
  }

  public boolean getAllowCompoundDoc(){
    return allowCompoundDoc;
  }
  
  public void setAllowPaperDoc(boolean allowPaperDoc){
    this.allowPaperDoc = allowPaperDoc;
  }

  public boolean getAllowPaperDoc(){
    return allowPaperDoc;
  }

  public void setAllowReset(boolean allowReset){
    this.allowReset = allowReset;
  }

  public boolean getAllowReset(){
    return allowReset;
  }  
  
  public String getIsMyFavorite(){
    return this.isMyFavorite;
  }

  public void setIsMyFavorite(String isMyFavorite){
    this.isMyFavorite= isMyFavorite;
  }
  
  public Integer getArrayIndex(){
    return this.arrayIndex;
  }

  public void setArrayIndex(Integer arrayIndex){
    this.arrayIndex= arrayIndex;
  }
  
  public void setAllowEmailDoc(boolean allowEmailDoc){
    this.allowEmailDoc = allowEmailDoc;
  }

  public boolean getAllowEmailDoc(){
    return allowEmailDoc;
  }
    
  public void setAllowFormDoc(boolean allowFormDoc){
    this.allowFormDoc = allowFormDoc;
  }

  public boolean getAllowFormDoc(){
    return allowFormDoc;
  }
      
  public void setAllowFlowDoc(boolean allowFlowDoc){
    this.allowFlowDoc = allowFlowDoc;
  }

  public boolean getAllowFlowDoc(){
    return allowFlowDoc;
  }
  
  /** overriden parent's doStartTag
   * @return
   * @throws JspException
   */
  public int doStartTag() throws javax.servlet.jsp.JspException {
    String actionMode = (String)pageContext.getRequest().getAttribute(GlobalConstant.ACTION_MODE);
    int retValue = SKIP_BODY;
    JspWriter out = pageContext.getOut();
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Connection conn = (Connection)this.pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    DocumentRetrievalManager documentRetrievalManager = new DocumentRetrievalManager(sessionContainer,conn);
    
    try {
      String sPropertyName = this.getProperty();
      Object value;
      String sHiddenStr="";

      // display value
      if(Utility.isEmpty(this.getArrayIndex())){
        this.setProperty(sPropertyName);
      }else{
        this.setProperty(sPropertyName + "_" + this.getArrayIndex().intValue());
      }
      
      String sDefaultValue = "";
      String arrayIndex = String.valueOf(getArrayIndex() );
      //display the location
      if(Utility.isEmpty(this.getArrayIndex())){
        sDefaultValue = (String)this.pageContext.getRequest().getAttribute("location");
      }else{
        if (!Utility.isEmpty(this.getHiddenParentIDValue())){
          sDefaultValue = documentRetrievalManager.getLocationPath(new Integer(this.getHiddenParentIDValue()));
        }else{
          sDefaultValue = "";
        }
      }
      
      out.print("<div class=\"div-1-a\">");
      
      this.setValue(sDefaultValue);
      retValue = super.doStartTag();
      this.setProperty(sPropertyName);
      
      out.print("</div>");
      
      if (!GlobalConstant.NAV_MODE_VIEW.equals(actionMode)) {
        String onClick = "";
        if(!disabled){
          if(Utility.isEmpty(this.getArrayIndex())){
            onClick = "return(popupFolderSelectionPanel('" + sHiddenRootIDValue + "', '" + sHiddenParentIDValue + "', '" + sPropertyName +
                  "', '" + sHiddenRootID + "', '" + sHiddenParentID + "','"+ sRefreshCopyMove +"','"+allowCompoundDoc+"','"+allowPaperDoc+"','"+allowReset+"','"+this.getIsMyFavorite()+"','','"+this.getAllowEmailDoc()+"','"+allowFormDoc+"','"+allowFlowDoc+"',null,null,null,null,null,'" + allowPersonalOnly + "','" + allowPublicOnly + "'))";
          }else{
            onClick = "return(popupFolderSelectionPanel('" + sHiddenRootIDValue + "', '" + sHiddenParentIDValue + "', '" + sPropertyName + "_" + this.getArrayIndex().intValue() +
                  "', '" + sHiddenRootID + "', '" + sHiddenParentID + "','"+ sRefreshCopyMove +"','"+allowCompoundDoc+"','"+allowPaperDoc+"','"+allowReset+"','"+this.getIsMyFavorite()+"','"+ arrayIndex + "','"+allowEmailDoc+"','"+allowFormDoc+"','"+allowFlowDoc+"',null,null,null,null,null,'" + allowPersonalOnly + "','" + allowPublicOnly + "'))";
          }
          out.print(TagUtil.renderStandardButton(pageContext, "dms.label.browse", onClick, null));
        }else{
          StandardButtonTag buttonTag = new StandardButtonTag();
          buttonTag.setPageContext(pageContext);
          buttonTag.setLabel("dms.label.browse");
          buttonTag.setDisable(true);
          out.println(TagUtil.renderTag(pageContext, buttonTag));
        }
      }
      //setup the hidden rootID and parentID value
      out.print("<input type=\"hidden\" name=\""+sHiddenRootID+"\" value=\""+sHiddenRootIDValue+"\">");
      out.print("<input type=\"hidden\" name=\""+sHiddenParentID+"\" value=\""+sHiddenParentIDValue+"\">");

      return retValue;
    }catch(Exception e) {
      log.error(e, e);
      throw new JspException(e);
    }
  }

  public boolean isAllowPersonalOnly() {
    return allowPersonalOnly;
  }

  public void setAllowPersonalOnly(boolean allowPersonalOnly) {
    this.allowPersonalOnly = allowPersonalOnly;
  }

  public boolean isAllowPublicOnly() {
    return allowPublicOnly;
  }

  public void setAllowPublicOnly(boolean allowPublicOnly) {
    this.allowPublicOnly = allowPublicOnly;
  }

}